package cn.fsw.pethome.org.service.impl;

import cn.fsw.pethome.base.service.impl.BaseServiceImpl;
import cn.fsw.pethome.constants.BaseConstants;
import cn.fsw.pethome.org.domain.Employee;
import cn.fsw.pethome.org.domain.Shop;
import cn.fsw.pethome.org.domain.ShopAuditLog;
import cn.fsw.pethome.org.domain.po.ShopCO;
import cn.fsw.pethome.org.domain.po.ShopPO;
import cn.fsw.pethome.org.domain.po.ShopVo;
import cn.fsw.pethome.org.mapper.EmployeeMapper;
import cn.fsw.pethome.org.mapper.ShopAuditLogMapper;
import cn.fsw.pethome.org.mapper.ShopMapper;
import cn.fsw.pethome.org.service.IEmployeeService;
import cn.fsw.pethome.org.service.IShopService;
import cn.fsw.pethome.util.BaiduAuditUtils;
import cn.fsw.pethome.util.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fsw
 * @since 2022-10-09
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        // 1.判断参数 ,这里省略部分判断
        if(shop.getName() == null){
            throw new BusinessException("店铺名字不能为空");
        }
        if(shop.getTel() == null){
            throw new BusinessException("店铺座机不能为空");
        }
        if(shop.getAddress() == null){
            throw new BusinessException("店铺地址不能为空");
        }
        if (!BaiduAuditUtils.TextCensor(shop.getName())) {
            throw new BusinessException("您填写的信息不合规,请重新填写");
        }
        // 2.判断当前店铺是否已经被注册
        Long countName = shopMapper.countName(shop.getName());
            if (countName > 0){
                throw new BusinessException("店铺名称已存在");
            }
        Long countTel = shopMapper.countTel(shop.getTel());
        if (countTel > 0){
            throw new BusinessException("座机号码已存在");
        }
        Long countAddress = shopMapper.countAddress(shop.getAddress());
        if (countAddress > 0){
            throw new BusinessException("店铺地址已存在");
        }
        //3.对参数进行合法的校验 例如 手机号、用户名、地址等等
        // 开头的"1"代表第一位为数字1，"[3-9]"代表第二位可以为3、4、5、6、7、8、9其中之一，"\\d{9}"代表后面是9位数字
        String regex = "1[3-9]\\d{9}";
        // 字符串变量的matches方法返回正则表达式对该串的检验结果，true表示符合字符串规则，false表示不符合规则
        if(!shop.getAdmin().getPhone().matches(regex)){
            throw new BusinessException("手机号错误");
        }
        if (shop.getAdmin().getUsername() == null){
            throw new BusinessException("账号不能为空");
        }
        // 4.保存管理员
        employeeService.add(shop.getAdmin());
        //5.保存商家到shop 设置管理员的外键(id)
        shop.setAdminId(shop.getAdmin().getId());
        shopMapper.add(shop);
        shop.getAdmin().setShop_id(shop.getId());
        employeeService.update(shop.getAdmin());
    }

    /*审核通过*/
    @Override
    @Transactional
    public void pass(ShopAuditLog shopAuditLog) {
        /*将审核状态添加到shopAuditLog
        * */
        shopAuditLog.setState(BaseConstants.Shop.STATE_WAIT_ACTIVE);
        shopAuditLog.setAuditId(333L);
        shopAuditLogMapper.add(shopAuditLog);
        /*修改shop的审核状态
        * */
        Shop shop = shopMapper.selectById(shopAuditLog.getShopId());
        shop.setState(BaseConstants.Shop.STATE_WAIT_ACTIVE);
        shopMapper.update(shop);
        /*把审核结果发送到店铺管理员的邮箱
        * */
        String msg = "<h1>恭喜您审核已通过,点击链接激活店铺</h1>"+"<a href='http://localhost:8080/shop/activation/"+shop.getId()+"'>激活</a>";
        //查询管理员邮箱
        Employee employee = employeeMapper.selectById(shop.getAdminId());
        sendEmail(msg,shop.getAdminId(),employee.getEmail());

    }
    /*激活*/
    @Override
    @Transactional
    public void activation(Long id) {
        Shop shop = shopMapper.selectById(id);
        shop.setState(BaseConstants.Shop.STATE_ACTIVE_SUCCESS);
        shopMapper.update(shop);
    }

    /*审核驳回*/
    @Override
    @Transactional
    public void reject(ShopAuditLog shopAuditLog) {
        /*将审核状态添加到shopAuditLog
         * */
        shopAuditLog.setState(BaseConstants.Shop.STATE_REJECT_AUDIT);
        shopAuditLog.setAuditId(333L);
        shopAuditLogMapper.add(shopAuditLog);
        /*修改shop的审核状态
         * */
        Shop shop = shopMapper.selectById(shopAuditLog.getShopId());
        shop.setState(BaseConstants.Shop.STATE_REJECT_AUDIT);
        shopMapper.update(shop);
        /*把审核结果发送到店铺管理员的邮箱
         * */
        //查询管理员邮箱
        Employee employee = employeeMapper.selectById(shop.getAdminId());
        sendEmail(shopAuditLog.getNote(),shop.getAdminId(),employee.getEmail());
    }
    /*批量添加*/
    @Override
    public void batchAdd(List<Shop> list) {
        shopMapper.batchAdd(list);
    }
    /*根据注册时间统计店铺注册数量*/
    @Override
    public List<ShopPO> chartLine() {
        return shopMapper.chartLine();
    }
    /*按照状态统计店铺数量*/
    @Override
    public List<ShopVo> chartColumn() {
        return shopMapper.chartColumn();
    }
    /*根据地址统计店铺数量*/
    @Override
    public List<ShopCO> chartBar() {
        return shopMapper.chartBar();
    }

    /*发送邮箱的方法
    * */
    private void sendEmail(String msg,Long admin_id,String email){
        try {
            //创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("1754214044@qq.com");
            helper.setSubject("宠物之家店铺入驻审核结果通知");
            //第二个参数 true 代表是否是一个html内容
            helper.setText(msg, true);
        /*//添加附件
        helper.addAttachment("文件名.jpg",new File("C:\\aa.jpg"));
        helper.addAttachment("压缩文件", new File("C:\\resources.zip"));*/
            //收件人
            helper.setTo(email);
            //发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
